package com.zyzd.retrofit.base;

import android.arch.lifecycle.Lifecycle;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.AutoDisposeConverter;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.zyzd.retrofit.R;

/**
 * Created by lzy on 2018/4/28 0028.
 * description ${MVP Fragment 基类}
 */

public abstract class BaseMvpFragment<P extends BaseContract.IPresenter> extends BaseFragment implements BaseContract.IView{

    protected P mPresenter;

    public BaseMvpFragment() {
        mPresenter = createPresenter();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        mPresenter.attachView(this);
        if (mPresenter == null) {
            throw new RuntimeException(getString(R.string.common_you_should_initialize_a_presenter));
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    public <X> AutoDisposeConverter<X> bindAutoDispose() {
        return AutoDispose.autoDisposable(AndroidLifecycleScopeProvider
                .from(this, Lifecycle.Event.ON_DESTROY));
    }

    protected abstract P createPresenter();

}
